import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from "@/views/Layout";
import Cookies from "js-cookie";

Vue.use(VueRouter)

const routes = [
    //  ==========================login================================
    //一级路由
    {path: '/login', name: 'Login', component: () => import('@/views/login/Login.vue')},
    //  ==========================Home=================================
    //一级路由
    {path: '/', name: 'Layout', component: Layout, redirect: 'home',
        children: [     //二级路由
            //  ==========================主页=================================
            {path: 'home', name: 'Home', component: () => import('@/views/home/Home.vue')},
            //  ==========================User=================================
            {path: 'user', name: 'User', component: () => import( '../views/user/User.vue')},
            {path: 'addUser', name: 'AddUser', component: () => import( '../views/user/AddUser.vue')},
            {path: 'editUser', name: 'EditUser', component: () => import( '../views/user/EditUser.vue')},
            //  ==========================Admin=================================
            {path: '/admin', name: 'Admin', component: () => import( '../views/admin/Admin.vue')},
            {path: '/addAdmin', name: 'AddAdmin', component: () => import( '../views/admin/AddAdmin.vue')},
            {path: '/editAdmin', name: 'EditAdmin', component: () => import( '../views/admin/EditAdmin.vue')},
            //  ==========================Category=================================
            {path: '/category', name: 'Category', component: () => import( '../views/category/Category.vue')},
            {path: '/addCategory', name: 'AddCategory', component: () => import( '../views/category/AddCategory.vue')},
            {path: '/editCategory', name: 'EditCategory', component: () => import( '../views/category/EditCategory.vue')},
            //  ==========================Book=================================
            {path: '/book', name: 'Book', component: () => import( '../views/book/Book.vue')},
            {path: '/addBook', name: 'AddBook', component: () => import( '../views/book/AddBook.vue')},
            {path: '/editBook', name: 'EditBook', component: () => import( '../views/book/EditBook.vue')},
            //  ==========================Borrow=================================
            {path: '/borrow', name: 'Borrow', component: () => import( '../views/borrow/Borrow.vue')},
            {path: '/addBorrow', name: 'AddBorrow', component: () => import( '../views/borrow/AddBorrow.vue')},
            {path: '/editBorrow', name: 'EditBorrow', component: () => import( '../views/borrow/EditBorrow.vue')},
            //  ==========================ReturnBook=================================
            {path: '/returnbook', name: 'ReturnBook', component: () => import( '../views/returnbook/ReturnBook.vue')},
        ]
    },
    // ===========================404==================================
    //一级路由
    {path: '*', component: () => import( '@/views/404.vue')}
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

//路由守卫
router.beforeEach((to, from, next) => {
    if (to.path === '/login') next()    //如果将要到达的路由是login登录页，直接放行
    const admin = Cookies.get('admin')    //获取到当前admin数据
    if (!admin && to.path !== '/login') return next('/login')  //如果没有admin数据，而且将要跳转的路由不是login，则强制跳转到login
    next()      //当访问的主页的时候，并且cookie里面有admin数据，则直接放行
})

export default router
